#!/bin/sh
SWAPFILE=/media/usb/swapfile
SWAPSIZE=65536 #64Mb

check_usb()
{
	echo check usb device mounted
	USB=`df | grep /media/usb | awk '{ print $6 }'`

    if [ ! $USB ]; then
   		echo no USB device mounted
   		exit 1
   	else
   		echo usb device found
   	fi
}

create_swapfile()
{
	echo create swapfile
	dd if=/dev/zero of=$SWAPFILE bs=1024 count=$SWAPSIZE 2>/dev/null
	mkswap $SWAPFILE
}

start_swap()
{
	echo start swap
	swapon $SWAPFILE
}

# Check if USB device is mounted
check_usb

# Start swap
if [ -f $SWAPFILE ]; then
	echo swapfile found
	start_swap
else
	echo no swapfile found
	create_swapfile
	start_swap
fi

exit 0
